package com.caifu.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class SendShortMessage {

    private static final String accessKeyId = "LTAII0t04Yk8eIcw";

    private static final String secret="b0NsKygG6DzAqPHZS90gPZYr89yOHZ";

    /**
    * 
    * 功能描述：验证短信验证码
    * 
    * @Param
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/8/24 13:51
    */
    public static JSONObject checkVerifyCode(String phoneNumber, String verifyCode, HttpServletRequest request) throws Exception {
        JSONObject resultMap = new JSONObject();
        if("".equals(Constance.Null2EmptyTrim(verifyCode))){
            resultMap.put("code","0001");
            resultMap.put("codeDesc","验证码为空");
            return resultMap;
        }
        if("".equals(Constance.Null2EmptyTrim(phoneNumber))){
            resultMap.put("code","0002");
            resultMap.put("codeDesc","手机号为空");
            return resultMap;
        }
        String serviceCode = (String) request.getSession().getAttribute("verifyCode");
        String servicePhone = (String) request.getSession().getAttribute("verifyPhone");
        if(verifyCode.equals(serviceCode)&&phoneNumber.equals(servicePhone)){
            resultMap.put("code","0000");
            resultMap.put("codeDesc","验证码验证成功");
            return resultMap;
        }else {
            resultMap.put("code","0003");
            resultMap.put("codeDesc","手机号或验证码错误");
            return resultMap;
        }
    }
    /**
    * 
    * 功能描述：发送短信二维码
    * 
    * @Param  signName 短信签名  phoneNumber 手机号 templateCode模板编号 templateParam 参数
    * @Return      
    * @Exception   
    * @Author  GZ
    * @Date   2020/8/24 13:46
    */
    public static JSONObject sendShortMessage(String signName, String phoneNumber
            , String templateCode, JSONObject templateParam){
        JSONObject obj = new JSONObject();
        if(StringUtils.isBlank(signName)){
            obj.put("code","1001");
            obj.put("codeDesc","签名为空!");
            return obj;
        }
        if(StringUtils.isBlank(phoneNumber)){
            obj.put("code","1002");
            obj.put("codeDesc","手机号为空!");
            return obj;
        }
        if(StringUtils.isBlank(templateCode)){
            obj.put("code","1003");
            obj.put("codeDesc","模板为空!");
            return obj;
        }
        if(null==templateParam){
            obj.put("code","1004");
            obj.put("codeDesc","短信参数为空!");
            return obj;
        }
        //发送短信
        DefaultProfile profile = DefaultProfile
                .getProfile("cn-hangzhou", accessKeyId
                        , secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam.toString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            String resultInfo = response.getData();
            if(StringUtils.isBlank(resultInfo)){
                obj.put("code","9999");
                obj.put("codeDesc","发送失败!");
                return obj;
            }
            System.out.println(resultInfo);
            JSONObject resultJson = JSONObject.fromObject(resultInfo);
            if(null==resultJson){
                obj.put("code","9999");
                obj.put("codeDesc","发送失败!");
                return obj;
            }
            if(resultJson.containsKey("Code")){
                String messageCode =resultJson.getString("Code");
                if("OK".equals(messageCode)){
                    obj.put("code","0000");
                    obj.put("codeDesc","发送成功!");
                    return obj;
                }else {
                    obj.put("code","9999");
                    obj.put("codeDesc","发送失败!");
                    return obj;
                }
            }else {
                obj.put("code","9999");
                obj.put("codeDesc","发送失败!");
                return obj;
            }
        } catch (ServerException e) {
            e.printStackTrace();
            obj.put("code","9999");
            obj.put("codeDesc","发送失败!");
            return obj;
        } catch (ClientException e) {
            e.printStackTrace();
            obj.put("code","9999");
            obj.put("codeDesc","发送失败!");
            return obj;
        }
    }
}
